//
//  BCDebugEnterView.m
//  BCDebugKit
//
//  Created by admin on 2018/7/7.
//

#import "BCDebugEnterView.h"
#import <BCFoundation/BCFoundationUtils.h>
#import "BCDebugKitPrivate.h"

@implementation BCDebugEnterView
#pragma mark - system
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 1;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.numberOfLines = 3;
        self.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
        self.titleLabel.font = [UIFont systemFontOfSize:11];
        [self setImage:BCDebugImageNamed(@"bc_debug_icon") forState:UIControlStateNormal];
        //拖拽事件，移动view
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onMoveEvent:)];
        panRecognizer.cancelsTouchesInView = YES;
        [self addGestureRecognizer:panRecognizer];
        
        //        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapEvent:)];
        //        tapRecognizer.cancelsTouchesInView = NO;
        //        tapRecognizer.numberOfTapsRequired = 3;
        //        [self addGestureRecognizer:tapRecognizer];
        [self addTarget:self action:@selector(onClickEvent:) forControlEvents:UIControlEventTouchUpInside];
        //长按事件,隐藏view
        UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPressEvent:)];
        longPressRecognizer.cancelsTouchesInView = YES;
        longPressRecognizer.minimumPressDuration = 2;
        [longPressRecognizer requireGestureRecognizerToFail:panRecognizer];
        [self addGestureRecognizer:longPressRecognizer];
    }
    return self;
}

//MARK: - 刷新数据
- (void)reloadData {
    BOOL needWarning = false;
    NSMutableString *title = [[NSMutableString alloc] init];
    if (self.fps >= 0) {
        [title appendFormat:@"%df", (int)self.fps];
        if (self.fps < 30) {
            needWarning = true;
        }
    }
    if (self.ram >= 0) {
        if (![title isEqualToString:@""]) {
            [title appendString:@"\r\n"];
        }
//        [title appendFormat:@"%ldm",lroundf((float)self.ram)];
        [title appendFormat:@"%.1fm", self.ram];
        if (self.ram > 600) {
            needWarning = true;
        }
    }
    if (self.cpu >= 0) {
        if (![title isEqualToString:@""]) {
            [title appendString:@"\r\n"];
        }
        [title appendFormat:@"%d%%", (int)self.cpu];
        if (self.cpu > 80) {
            needWarning = true;
        }
    }
    if (![title isEqualToString:@""]) {
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4];
        [self setImage:nil forState:UIControlStateNormal];
        [self setTitle:(NSString *)title forState:UIControlStateNormal];
        [self setTitleColor:((needWarning)?UIColor.redColor:UIColor.whiteColor) forState:UIControlStateNormal];
    }
    else {
        self.backgroundColor = UIColor.clearColor;
        [self setImage:BCDebugImageNamed(@"bc_debug_icon") forState:UIControlStateNormal];
        [self setTitle:nil forState:UIControlStateNormal];
    }
}

//MARK: - touch event
- (void)onMoveEvent:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state != UIGestureRecognizerStateChanged) {
        return;
    }
    UIButton *button = (UIButton *)recognizer.view;
    CGPoint translation = [recognizer translationInView:button];
    button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:button];
}
- (void)onLongPressEvent:(UILongPressGestureRecognizer *)recognizer {
    if (recognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    [UIView animateWithDuration:2 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        self.hidden = true;
    }];
}
- (void)onTapEvent:(UITapGestureRecognizer *)recognizer {
    if (recognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    if (self.clickBlock) {
        self.clickBlock();
    }
}
- (void)onClickEvent:(UIButton *)sender {
    if (self.clickBlock) {
        self.clickBlock();
    }
}
@end
